package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.api.service.BfjlspService;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwbnT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 非商机拜访记录审核
 * Created by wenchuan.li on 2019/3/27.
 */
@RestController
@RequestMapping("api/wx/bfjlsp")
public class BfjlspController extends ControllerSupport {

    @Autowired
    BfjlspService bfjlspService;


    @Autowired
    GwbService gwbService;

    @Autowired
    SystemServiceImpl systemService;

    /**
     * 获取非商机拜访记录审核
     *
     * @return
     */
    @RequestMapping(value = "getBfjlspList")
    public ResponseData getBfjlspList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        List<Dto> list = new ArrayList<Dto>();

        try {
            inDto.put("USERID", currentUser.getID());

            //客户权限
            inDto.put("GWAK006", "1");
            Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list = (List<Dto>) gwb001.get("CUSTOMERS");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            List<Dto> listOrg = (List<Dto>) sys003.get("ORGS");
            listOrg.remove(listOrg.size() - 1);
            inDto.put("ORGS", listOrg);

            list = InvokeUtil.invokeBeanMethod("bfjlspMapper,getBfjlspListPage",wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 确认、取消确认  update
     *
     * @return
     */
    @RequestMapping("updateByKey")
    public ResponseData updateByKey(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        JSONObject inJson = new JSONObject();
        JSONArray characteristics = new JSONArray();
        try {
            inJson = JSONObject.parseObject(inDto.getString("obj"));
            characteristics = inJson.getJSONArray("CHARACTERISTICS");
            for (int i = 0; i < characteristics.size(); i++) {
                JSONObject item = characteristics.getJSONObject(i);
                GwbnT gwbn_tPO = CommonUtils.toJavaClass(item,GwbnT.class);

                UpdateWrapper<GwbnT> wrapper = new UpdateWrapper<>();
                wrapper.eq("GWBNENT",inDto.getString("ENT"));
                wrapper.eq("GWBNID",gwbn_tPO.getGWBNID());
                wrapper.eq("GWBNSEQ",gwbn_tPO.getGWBNSEQ());
                gwbn_tPO.update(wrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 获取详细信息
     *
     * @return
     */
    @RequestMapping(value = "getBfjlspInfo")
    public ResponseData getBfjlspInfo(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        try {
            list = bfjlspService.getBfjlspInfo(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;

        }
        return SUCCESS(list);
    }


    /**
     * 提交资料  update
     *
     * @return
     */
    @RequestMapping("updateInfo")
    public ResponseData updateInfo(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        GwbnT gwbn_tPO = new GwbnT();
        try {
            gwbn_tPO.setGWBNENT(currentUser.getDBC());
            gwbn_tPO.setGWBNID(inDto.getString("GWBNID"));
            gwbn_tPO.setGWBNSEQ(inDto.getBigDecimal("GWBNSEQ"));
            gwbn_tPO.setGWBN012(inDto.getString("GWBN012"));
            gwbn_tPO.setGWBNMODU(currentUser.getID());

            UpdateWrapper<GwbnT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWBNID",inDto.getString("GWBNID"));
            wrapper.eq("GWBNSEQ",inDto.getBigDecimal("GWBNSEQ"));
            gwbn_tPO.update(wrapper);

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }


    /**
     * 获取拜访内容填写分类
     *
     * @return
     */
    @RequestMapping(value = "getGwbfList")
    public ResponseData getGwbfList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        try {
            inDto.put("GWBFLENT", inDto.getString("ENT"));
            list = bfjlspService.getGwbfList(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

}
